{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.preprocessing import OneHotEncoder,Binarizer\n",
    "from sklearn.model_selection import train_test_split\n",
    "import numpy as np\n",
    "from sklearn.linear_model import LogisticRegression,LinearRegression\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score,confusion_matrix,mean_squared_error,recall_score,roc_auc_score,precision_score,f1_score\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "import joblib\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.metrics import RocCurveDisplay\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import precision_recall_curve\n",
    "from sklearn.feature_extraction.text import CountVectorizer,TfidfVectorizer\n",
    "import jieba\n",
    "from sklearn.cluster import KMeans\n",
    "from sklearn.impute import SimpleImputer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "      Daily Time Spent on Site  Age  Area Income  Daily Internet Usage  \\\n0                        62.26   32     69481.85                172.83   \n1                        41.73   31     61840.26                207.17   \n2                        44.40   30     57877.15                172.83   \n3                        59.88   28     56180.93                207.17   \n4                        49.21   30     54324.73                201.58   \n...                        ...  ...          ...                   ...   \n9995                     41.73   31     61840.26                207.17   \n9996                     41.73   28     51501.38                120.49   \n9997                     55.60   39     38067.08                124.44   \n9998                     46.61   50     43974.49                123.13   \n9999                     46.61   43     60575.99                198.45   \n\n                               Ad Topic Line             City  Gender  \\\n0            Decentralized real-time circuit         Lisafort    Male   \n1             Optional full-range projection  West Angelabury    Male   \n2        Total 5thgeneration standardization        Reyesfurt  Female   \n3                Balanced empowering success      New Michael  Female   \n4        Total 5thgeneration standardization     West Richard  Female   \n...                                      ...              ...     ...   \n9995          Profound executive flexibility  West Angelabury    Male   \n9996          Managed zero tolerance concept      Kennedyfurt    Male   \n9997          Intuitive exuding service-desk      North Randy  Female   \n9998        Realigned content-based leverage   North Samantha  Female   \n9999  Optimized upward-trending productivity     Port Jeffrey    Male   \n\n                           Country        Timestamp  Clicked on Ad  \n0     Svalbard & Jan Mayen Islands   2016/6/9 21:43              0  \n1                        Singapore  2016/1/16 17:56              0  \n2                       Guadeloupe  2016/6/29 10:50              0  \n3                           Zambia  2016/6/21 14:32              0  \n4                            Qatar  2016/7/21 10:54              1  \n...                            ...              ...            ...  \n9995                     Singapore    2016/1/3 3:22              1  \n9996                    Luxembourg  2016/5/28 12:20              0  \n9997                         Egypt   2016/1/5 11:53              0  \n9998                        Malawi    2016/4/4 7:07              1  \n9999      Northern Mariana Islands   2016/4/3 21:13              1  \n\n[10000 rows x 10 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Daily Time Spent on Site</th>\n      <th>Age</th>\n      <th>Area Income</th>\n      <th>Daily Internet Usage</th>\n      <th>Ad Topic Line</th>\n      <th>City</th>\n      <th>Gender</th>\n      <th>Country</th>\n      <th>Timestamp</th>\n      <th>Clicked on Ad</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>62.26</td>\n      <td>32</td>\n      <td>69481.85</td>\n      <td>172.83</td>\n      <td>Decentralized real-time circuit</td>\n      <td>Lisafort</td>\n      <td>Male</td>\n      <td>Svalbard &amp; Jan Mayen Islands</td>\n      <td>2016/6/9 21:43</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>41.73</td>\n      <td>31</td>\n      <td>61840.26</td>\n      <td>207.17</td>\n      <td>Optional full-range projection</td>\n      <td>West Angelabury</td>\n      <td>Male</td>\n      <td>Singapore</td>\n      <td>2016/1/16 17:56</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>44.40</td>\n      <td>30</td>\n      <td>57877.15</td>\n      <td>172.83</td>\n      <td>Total 5thgeneration standardization</td>\n      <td>Reyesfurt</td>\n      <td>Female</td>\n      <td>Guadeloupe</td>\n      <td>2016/6/29 10:50</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>59.88</td>\n      <td>28</td>\n      <td>56180.93</td>\n      <td>207.17</td>\n      <td>Balanced empowering success</td>\n      <td>New Michael</td>\n      <td>Female</td>\n      <td>Zambia</td>\n      <td>2016/6/21 14:32</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>49.21</td>\n      <td>30</td>\n      <td>54324.73</td>\n      <td>201.58</td>\n      <td>Total 5thgeneration standardization</td>\n      <td>West Richard</td>\n      <td>Female</td>\n      <td>Qatar</td>\n      <td>2016/7/21 10:54</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>9995</th>\n      <td>41.73</td>\n      <td>31</td>\n      <td>61840.26</td>\n      <td>207.17</td>\n      <td>Profound executive flexibility</td>\n      <td>West Angelabury</td>\n      <td>Male</td>\n      <td>Singapore</td>\n      <td>2016/1/3 3:22</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9996</th>\n      <td>41.73</td>\n      <td>28</td>\n      <td>51501.38</td>\n      <td>120.49</td>\n      <td>Managed zero tolerance concept</td>\n      <td>Kennedyfurt</td>\n      <td>Male</td>\n      <td>Luxembourg</td>\n      <td>2016/5/28 12:20</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>9997</th>\n      <td>55.60</td>\n      <td>39</td>\n      <td>38067.08</td>\n      <td>124.44</td>\n      <td>Intuitive exuding service-desk</td>\n      <td>North Randy</td>\n      <td>Female</td>\n      <td>Egypt</td>\n      <td>2016/1/5 11:53</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>9998</th>\n      <td>46.61</td>\n      <td>50</td>\n      <td>43974.49</td>\n      <td>123.13</td>\n      <td>Realigned content-based leverage</td>\n      <td>North Samantha</td>\n      <td>Female</td>\n      <td>Malawi</td>\n      <td>2016/4/4 7:07</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9999</th>\n      <td>46.61</td>\n      <td>43</td>\n      <td>60575.99</td>\n      <td>198.45</td>\n      <td>Optimized upward-trending productivity</td>\n      <td>Port Jeffrey</td>\n      <td>Male</td>\n      <td>Northern Mariana Islands</td>\n      <td>2016/4/3 21:13</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n<p>10000 rows × 10 columns</p>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(\"C:\\\\Users\\\\Administrator\\\\Desktop\\\\月考练习算法题 (2)\\\\月考练习算法题\\\\第4套（修改3）\\\\专高6月考-04附件\\\\网站点击预测.csv\")\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 10000 entries, 0 to 9999\n",
      "Data columns (total 10 columns):\n",
      " #   Column                    Non-Null Count  Dtype  \n",
      "---  ------                    --------------  -----  \n",
      " 0   Daily Time Spent on Site  10000 non-null  float64\n",
      " 1   Age                       10000 non-null  int64  \n",
      " 2   Area Income               10000 non-null  float64\n",
      " 3   Daily Internet Usage      10000 non-null  float64\n",
      " 4   Ad Topic Line             10000 non-null  object \n",
      " 5   City                      10000 non-null  object \n",
      " 6   Gender                    10000 non-null  object \n",
      " 7   Country                   10000 non-null  object \n",
      " 8   Timestamp                 10000 non-null  object \n",
      " 9   Clicked on Ad             10000 non-null  int64  \n",
      "dtypes: float64(3), int64(2), object(5)\n",
      "memory usage: 781.4+ KB\n"
     ]
    },
    {
     "data": {
      "text/plain": "      Daily Time Spent on Site  Age  Area Income  Daily Internet Usage  \\\n0                        62.26   32     69481.85                172.83   \n1                        41.73   31     61840.26                207.17   \n2                        44.40   30     57877.15                172.83   \n3                        59.88   28     56180.93                207.17   \n4                        49.21   30     54324.73                201.58   \n...                        ...  ...          ...                   ...   \n9995                     41.73   31     61840.26                207.17   \n9996                     41.73   28     51501.38                120.49   \n9997                     55.60   39     38067.08                124.44   \n9998                     46.61   50     43974.49                123.13   \n9999                     46.61   43     60575.99                198.45   \n\n      Ad Topic Line  City  Gender  Country  Timestamp  Clicked on Ad  \n0                 0     0       0        0          0              0  \n1                 1     1       0        1          1              0  \n2                 2     2       1        2          2              0  \n3                 3     3       1        3          3              0  \n4                 2     4       1        4          4              1  \n...             ...   ...     ...      ...        ...            ...  \n9995            266     1       0        1        279              1  \n9996             61   166       0       53        250              0  \n9997             15    15       1      125        256              0  \n9998            176   205       1      133        320              1  \n9999              5     6       0       16          6              1  \n\n[10000 rows x 10 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Daily Time Spent on Site</th>\n      <th>Age</th>\n      <th>Area Income</th>\n      <th>Daily Internet Usage</th>\n      <th>Ad Topic Line</th>\n      <th>City</th>\n      <th>Gender</th>\n      <th>Country</th>\n      <th>Timestamp</th>\n      <th>Clicked on Ad</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>62.26</td>\n      <td>32</td>\n      <td>69481.85</td>\n      <td>172.83</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>41.73</td>\n      <td>31</td>\n      <td>61840.26</td>\n      <td>207.17</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>44.40</td>\n      <td>30</td>\n      <td>57877.15</td>\n      <td>172.83</td>\n      <td>2</td>\n      <td>2</td>\n      <td>1</td>\n      <td>2</td>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>59.88</td>\n      <td>28</td>\n      <td>56180.93</td>\n      <td>207.17</td>\n      <td>3</td>\n      <td>3</td>\n      <td>1</td>\n      <td>3</td>\n      <td>3</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>49.21</td>\n      <td>30</td>\n      <td>54324.73</td>\n      <td>201.58</td>\n      <td>2</td>\n      <td>4</td>\n      <td>1</td>\n      <td>4</td>\n      <td>4</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>9995</th>\n      <td>41.73</td>\n      <td>31</td>\n      <td>61840.26</td>\n      <td>207.17</td>\n      <td>266</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n      <td>279</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9996</th>\n      <td>41.73</td>\n      <td>28</td>\n      <td>51501.38</td>\n      <td>120.49</td>\n      <td>61</td>\n      <td>166</td>\n      <td>0</td>\n      <td>53</td>\n      <td>250</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>9997</th>\n      <td>55.60</td>\n      <td>39</td>\n      <td>38067.08</td>\n      <td>124.44</td>\n      <td>15</td>\n      <td>15</td>\n      <td>1</td>\n      <td>125</td>\n      <td>256</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>9998</th>\n      <td>46.61</td>\n      <td>50</td>\n      <td>43974.49</td>\n      <td>123.13</td>\n      <td>176</td>\n      <td>205</td>\n      <td>1</td>\n      <td>133</td>\n      <td>320</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9999</th>\n      <td>46.61</td>\n      <td>43</td>\n      <td>60575.99</td>\n      <td>198.45</td>\n      <td>5</td>\n      <td>6</td>\n      <td>0</td>\n      <td>16</td>\n      <td>6</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n<p>10000 rows × 10 columns</p>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.info()\n",
    "for col in df.columns:\n",
    "\tif df[col].dtype == \"object\":\n",
    "\t\tdf[col] = pd.factorize(df[col])[0]\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "Daily Time Spent on Site    0\nAge                         0\nArea Income                 0\nDaily Internet Usage        0\nAd Topic Line               0\nCity                        0\nGender                      0\nCountry                     0\nTimestamp                   0\nClicked on Ad               0\ndtype: int64"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "Daily Time Spent on Site    0\nAge                         0\nArea Income                 0\nDaily Internet Usage        0\nAd Topic Line               0\nCity                        0\nGender                      0\nCountry                     0\nTimestamp                   0\nClicked on Ad               0\ndtype: int64"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_encoders = {}\n",
    "for i in ['Ad Topic Line', 'City', 'Gender', 'Country', 'Timestamp']:\n",
    "\tlabel_encoders[i] = LabelEncoder()\n",
    "\tdf[i] = label_encoders[i].fit_transform(df[i])\n",
    "df\n",
    "# 处理缺失值，用均值填充\n",
    "\n",
    "df.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "      Daily Time Spent on Site   Age  Area Income  Daily Internet Usage  \\\n0                        62.26  32.0     69481.85                172.83   \n1                        41.73  31.0     61840.26                207.17   \n2                        44.40  30.0     57877.15                172.83   \n3                        59.88  28.0     56180.93                207.17   \n4                        49.21  30.0     54324.73                201.58   \n...                        ...   ...          ...                   ...   \n9995                     41.73  31.0     61840.26                207.17   \n9996                     41.73  28.0     51501.38                120.49   \n9997                     55.60  39.0     38067.08                124.44   \n9998                     46.61  50.0     43974.49                123.13   \n9999                     46.61  43.0     60575.99                198.45   \n\n      Ad Topic Line   City  Gender  Country  Timestamp  Clicked on Ad  \n0              96.0  234.0     1.0    174.0      505.0            0.0  \n1             301.0  460.0     1.0    166.0       21.0            0.0  \n2             484.0  379.0     0.0     71.0      487.0            0.0  \n3              24.0  269.0     0.0    205.0      469.0            0.0  \n4             484.0  495.0     0.0    149.0      538.0            1.0  \n...             ...    ...     ...      ...        ...            ...  \n9995          353.0  460.0     1.0    166.0       59.0            1.0  \n9996          241.0  177.0     1.0    105.0      404.0            0.0  \n9997          222.0  316.0     0.0     48.0       74.0            0.0  \n9998          396.0  321.0     0.0    108.0      335.0            1.0  \n9999          300.0  355.0     1.0    136.0      327.0            1.0  \n\n[10000 rows x 10 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Daily Time Spent on Site</th>\n      <th>Age</th>\n      <th>Area Income</th>\n      <th>Daily Internet Usage</th>\n      <th>Ad Topic Line</th>\n      <th>City</th>\n      <th>Gender</th>\n      <th>Country</th>\n      <th>Timestamp</th>\n      <th>Clicked on Ad</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>62.26</td>\n      <td>32.0</td>\n      <td>69481.85</td>\n      <td>172.83</td>\n      <td>96.0</td>\n      <td>234.0</td>\n      <td>1.0</td>\n      <td>174.0</td>\n      <td>505.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>41.73</td>\n      <td>31.0</td>\n      <td>61840.26</td>\n      <td>207.17</td>\n      <td>301.0</td>\n      <td>460.0</td>\n      <td>1.0</td>\n      <td>166.0</td>\n      <td>21.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>44.40</td>\n      <td>30.0</td>\n      <td>57877.15</td>\n      <td>172.83</td>\n      <td>484.0</td>\n      <td>379.0</td>\n      <td>0.0</td>\n      <td>71.0</td>\n      <td>487.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>59.88</td>\n      <td>28.0</td>\n      <td>56180.93</td>\n      <td>207.17</td>\n      <td>24.0</td>\n      <td>269.0</td>\n      <td>0.0</td>\n      <td>205.0</td>\n      <td>469.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>49.21</td>\n      <td>30.0</td>\n      <td>54324.73</td>\n      <td>201.58</td>\n      <td>484.0</td>\n      <td>495.0</td>\n      <td>0.0</td>\n      <td>149.0</td>\n      <td>538.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>9995</th>\n      <td>41.73</td>\n      <td>31.0</td>\n      <td>61840.26</td>\n      <td>207.17</td>\n      <td>353.0</td>\n      <td>460.0</td>\n      <td>1.0</td>\n      <td>166.0</td>\n      <td>59.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>9996</th>\n      <td>41.73</td>\n      <td>28.0</td>\n      <td>51501.38</td>\n      <td>120.49</td>\n      <td>241.0</td>\n      <td>177.0</td>\n      <td>1.0</td>\n      <td>105.0</td>\n      <td>404.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>9997</th>\n      <td>55.60</td>\n      <td>39.0</td>\n      <td>38067.08</td>\n      <td>124.44</td>\n      <td>222.0</td>\n      <td>316.0</td>\n      <td>0.0</td>\n      <td>48.0</td>\n      <td>74.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>9998</th>\n      <td>46.61</td>\n      <td>50.0</td>\n      <td>43974.49</td>\n      <td>123.13</td>\n      <td>396.0</td>\n      <td>321.0</td>\n      <td>0.0</td>\n      <td>108.0</td>\n      <td>335.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>9999</th>\n      <td>46.61</td>\n      <td>43.0</td>\n      <td>60575.99</td>\n      <td>198.45</td>\n      <td>300.0</td>\n      <td>355.0</td>\n      <td>1.0</td>\n      <td>136.0</td>\n      <td>327.0</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n<p>10000 rows × 10 columns</p>\n</div>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imputer = SimpleImputer(strategy='mean')\n",
    "df = pd.DataFrame(imputer.fit_transform(df), columns=df.columns)\n",
    "df\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "      Daily Time Spent on Site   Age  Area Income  Daily Internet Usage  \\\n0                        62.26  32.0     69481.85                172.83   \n1                        41.73  31.0     61840.26                207.17   \n2                        44.40  30.0     57877.15                172.83   \n3                        59.88  28.0     56180.93                207.17   \n4                        49.21  30.0     54324.73                201.58   \n...                        ...   ...          ...                   ...   \n9995                     41.73  31.0     61840.26                207.17   \n9996                     41.73  28.0     51501.38                120.49   \n9997                     55.60  39.0     38067.08                124.44   \n9998                     46.61  50.0     43974.49                123.13   \n9999                     46.61  43.0     60575.99                198.45   \n\n      Ad Topic Line   City  Gender  Country  Clicked on Ad  \n0              96.0  234.0     1.0    174.0            0.0  \n1             301.0  460.0     1.0    166.0            0.0  \n2             484.0  379.0     0.0     71.0            0.0  \n3              24.0  269.0     0.0    205.0            0.0  \n4             484.0  495.0     0.0    149.0            1.0  \n...             ...    ...     ...      ...            ...  \n9995          353.0  460.0     1.0    166.0            1.0  \n9996          241.0  177.0     1.0    105.0            0.0  \n9997          222.0  316.0     0.0     48.0            0.0  \n9998          396.0  321.0     0.0    108.0            1.0  \n9999          300.0  355.0     1.0    136.0            1.0  \n\n[10000 rows x 9 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Daily Time Spent on Site</th>\n      <th>Age</th>\n      <th>Area Income</th>\n      <th>Daily Internet Usage</th>\n      <th>Ad Topic Line</th>\n      <th>City</th>\n      <th>Gender</th>\n      <th>Country</th>\n      <th>Clicked on Ad</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>62.26</td>\n      <td>32.0</td>\n      <td>69481.85</td>\n      <td>172.83</td>\n      <td>96.0</td>\n      <td>234.0</td>\n      <td>1.0</td>\n      <td>174.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>41.73</td>\n      <td>31.0</td>\n      <td>61840.26</td>\n      <td>207.17</td>\n      <td>301.0</td>\n      <td>460.0</td>\n      <td>1.0</td>\n      <td>166.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>44.40</td>\n      <td>30.0</td>\n      <td>57877.15</td>\n      <td>172.83</td>\n      <td>484.0</td>\n      <td>379.0</td>\n      <td>0.0</td>\n      <td>71.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>59.88</td>\n      <td>28.0</td>\n      <td>56180.93</td>\n      <td>207.17</td>\n      <td>24.0</td>\n      <td>269.0</td>\n      <td>0.0</td>\n      <td>205.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>49.21</td>\n      <td>30.0</td>\n      <td>54324.73</td>\n      <td>201.58</td>\n      <td>484.0</td>\n      <td>495.0</td>\n      <td>0.0</td>\n      <td>149.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>9995</th>\n      <td>41.73</td>\n      <td>31.0</td>\n      <td>61840.26</td>\n      <td>207.17</td>\n      <td>353.0</td>\n      <td>460.0</td>\n      <td>1.0</td>\n      <td>166.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>9996</th>\n      <td>41.73</td>\n      <td>28.0</td>\n      <td>51501.38</td>\n      <td>120.49</td>\n      <td>241.0</td>\n      <td>177.0</td>\n      <td>1.0</td>\n      <td>105.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>9997</th>\n      <td>55.60</td>\n      <td>39.0</td>\n      <td>38067.08</td>\n      <td>124.44</td>\n      <td>222.0</td>\n      <td>316.0</td>\n      <td>0.0</td>\n      <td>48.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>9998</th>\n      <td>46.61</td>\n      <td>50.0</td>\n      <td>43974.49</td>\n      <td>123.13</td>\n      <td>396.0</td>\n      <td>321.0</td>\n      <td>0.0</td>\n      <td>108.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>9999</th>\n      <td>46.61</td>\n      <td>43.0</td>\n      <td>60575.99</td>\n      <td>198.45</td>\n      <td>300.0</td>\n      <td>355.0</td>\n      <td>1.0</td>\n      <td>136.0</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n<p>10000 rows × 9 columns</p>\n</div>"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.drop(columns=['Timestamp'], inplace=True)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "      Daily Time Spent on Site   Age  Area Income  Daily Internet Usage  \\\n8371                     70.29  36.0     59886.58                115.26   \n5027                     51.24  32.0     55424.24                172.83   \n9234                     55.60  35.0     73889.99                236.87   \n3944                     40.47  35.0     25603.93                236.87   \n6862                     40.04  34.0     40183.75                168.34   \n...                        ...   ...          ...                   ...   \n5734                     82.07  41.0     57846.68                126.39   \n5191                     89.00  36.0     50628.31                126.39   \n5390                     78.84  35.0     25603.93                236.87   \n860                      59.51  30.0     57877.15                138.71   \n7270                     59.05  33.0     52736.33                113.12   \n\n      Ad Topic Line   City  Gender  Country  \n8371          483.0   76.0     1.0     81.0  \n5027          161.0  234.0     0.0     42.0  \n9234          302.0  516.0     0.0    192.0  \n3944          302.0  306.0     0.0    159.0  \n6862           45.0  147.0     0.0    158.0  \n...             ...    ...     ...      ...  \n5734          421.0  484.0     0.0    149.0  \n5191          388.0  248.0     1.0    187.0  \n5390          527.0  516.0     0.0    192.0  \n860           396.0  515.0     1.0     20.0  \n7270            1.0   33.0     1.0    200.0  \n\n[6700 rows x 8 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Daily Time Spent on Site</th>\n      <th>Age</th>\n      <th>Area Income</th>\n      <th>Daily Internet Usage</th>\n      <th>Ad Topic Line</th>\n      <th>City</th>\n      <th>Gender</th>\n      <th>Country</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>8371</th>\n      <td>70.29</td>\n      <td>36.0</td>\n      <td>59886.58</td>\n      <td>115.26</td>\n      <td>483.0</td>\n      <td>76.0</td>\n      <td>1.0</td>\n      <td>81.0</td>\n    </tr>\n    <tr>\n      <th>5027</th>\n      <td>51.24</td>\n      <td>32.0</td>\n      <td>55424.24</td>\n      <td>172.83</td>\n      <td>161.0</td>\n      <td>234.0</td>\n      <td>0.0</td>\n      <td>42.0</td>\n    </tr>\n    <tr>\n      <th>9234</th>\n      <td>55.60</td>\n      <td>35.0</td>\n      <td>73889.99</td>\n      <td>236.87</td>\n      <td>302.0</td>\n      <td>516.0</td>\n      <td>0.0</td>\n      <td>192.0</td>\n    </tr>\n    <tr>\n      <th>3944</th>\n      <td>40.47</td>\n      <td>35.0</td>\n      <td>25603.93</td>\n      <td>236.87</td>\n      <td>302.0</td>\n      <td>306.0</td>\n      <td>0.0</td>\n      <td>159.0</td>\n    </tr>\n    <tr>\n      <th>6862</th>\n      <td>40.04</td>\n      <td>34.0</td>\n      <td>40183.75</td>\n      <td>168.34</td>\n      <td>45.0</td>\n      <td>147.0</td>\n      <td>0.0</td>\n      <td>158.0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>5734</th>\n      <td>82.07</td>\n      <td>41.0</td>\n      <td>57846.68</td>\n      <td>126.39</td>\n      <td>421.0</td>\n      <td>484.0</td>\n      <td>0.0</td>\n      <td>149.0</td>\n    </tr>\n    <tr>\n      <th>5191</th>\n      <td>89.00</td>\n      <td>36.0</td>\n      <td>50628.31</td>\n      <td>126.39</td>\n      <td>388.0</td>\n      <td>248.0</td>\n      <td>1.0</td>\n      <td>187.0</td>\n    </tr>\n    <tr>\n      <th>5390</th>\n      <td>78.84</td>\n      <td>35.0</td>\n      <td>25603.93</td>\n      <td>236.87</td>\n      <td>527.0</td>\n      <td>516.0</td>\n      <td>0.0</td>\n      <td>192.0</td>\n    </tr>\n    <tr>\n      <th>860</th>\n      <td>59.51</td>\n      <td>30.0</td>\n      <td>57877.15</td>\n      <td>138.71</td>\n      <td>396.0</td>\n      <td>515.0</td>\n      <td>1.0</td>\n      <td>20.0</td>\n    </tr>\n    <tr>\n      <th>7270</th>\n      <td>59.05</td>\n      <td>33.0</td>\n      <td>52736.33</td>\n      <td>113.12</td>\n      <td>1.0</td>\n      <td>33.0</td>\n      <td>1.0</td>\n      <td>200.0</td>\n    </tr>\n  </tbody>\n</table>\n<p>6700 rows × 8 columns</p>\n</div>"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = df.iloc[:, :-1]\n",
    "y = df.iloc[:, -1]\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 1. 1. ... 1. 0. 0.]\n",
      "[1. 1. 1. ... 1. 0. 1.]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "F:\\python38\\lib\\site-packages\\sklearn\\linear_model\\_logistic.py:460: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
      "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
      "\n",
      "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
      "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
      "Please also refer to the documentation for alternative solver options:\n",
      "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
      "  n_iter_i = _check_optimize_result(\n"
     ]
    }
   ],
   "source": [
    "#随机森林建模\n",
    "model_rf = RandomForestClassifier(max_depth=50, min_samples_split=2, max_leaf_nodes=None)\n",
    "#逻辑回归建模\n",
    "model_lr = LogisticRegression(C=1.0, max_iter=100, n_jobs=1)\n",
    "\n",
    "#训练模型\n",
    "model_rf.fit(X_train, y_train)\n",
    "model_lr.fit(X_train, y_train)\n",
    "\n",
    "#预测结果\n",
    "rf_pred = model_rf.predict(X_test)\n",
    "lr_pred = model_lr.predict(X_test)\n",
    "\n",
    "print(rf_pred)\n",
    "print(lr_pred)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logiect--   acc:0.649090909090909 , precision:0.6439246263807668, recall:0.6189881324172393 ,f1:0.6312101910828025\n",
      "randomtree--   acc:0.826060606060606 , precision:0.8289557975656631, recall:0.8082448469706434 ,f1:0.8184693232131562\n"
     ]
    }
   ],
   "source": [
    "#评估 acc\n",
    "lr_acc = accuracy_score(y_test, lr_pred)\n",
    "rf_acc = accuracy_score(y_test, rf_pred)\n",
    "#precision\n",
    "lr_precision = precision_score(y_test, lr_pred)\n",
    "rf_precision = precision_score(y_test, rf_pred)\n",
    "#recall\n",
    "lr_recall = recall_score(y_test, lr_pred)\n",
    "rf_recall = recall_score(y_test, rf_pred)\n",
    "#f1\n",
    "lr_f1 = f1_score(y_test, lr_pred)\n",
    "rf_f1 = f1_score(y_test, rf_pred)\n",
    "\n",
    "print(f'logiect--   acc:{lr_acc} , precision:{lr_precision}, recall:{lr_recall} ,f1:{lr_f1}')\n",
    "print(f'randomtree--   acc:{rf_acc} , precision:{rf_precision}, recall:{rf_recall} ,f1:{rf_f1}')\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'max_depth': 70, 'max_samples': 0.9, 'n_estimators': 350}\n"
     ]
    },
    {
     "data": {
      "text/plain": "0.8284848484848485"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#randomTree\n",
    "rf_params = {\n",
    "    'n_estimators': [250, 300, 350],\n",
    "    'max_depth': [50, 60, 70],\n",
    "    'max_samples': [0.6, 0.9, 1]\n",
    "}\n",
    "rf_gridCV = GridSearchCV(model_rf, param_grid=rf_params, cv=5, scoring=None)\n",
    "\n",
    "rf_gridCV.fit(X_train, y_train)\n",
    "\n",
    "print(rf_gridCV.best_params_)\n",
    "#{'max_depth': 60, 'max_samples': 0.9, 'n_estimators': 300}\n",
    "\n",
    "best_rf_model = rf_gridCV.best_estimator_\n",
    "best_rf_pred = best_rf_model.predict(X_test)\n",
    "best_acc_rf = accuracy_score(y_test, best_rf_pred)\n",
    "best_acc_rf\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#LogisticRegression\n",
    "lr_params = {\n",
    "    'fit_intercept': [True, False],\n",
    "    'solver': ['liblinear', 'sag', 'lbfgs'],\n",
    "    'max_iter': [10, 30, 40],\n",
    "}\n",
    "lr_gridCV = GridSearchCV(model_lr, param_grid=lr_params, cv=5, scoring=None)\n",
    "\n",
    "lr_gridCV.fit(X_train, y_train)\n",
    "\n",
    "print(lr_gridCV.best_params_)\n",
    "#{'fit_intercept': 'True', 'max_iter': 30, 'solver': 'liblinear'}\n",
    "\n",
    "best_lr_model = lr_gridCV.best_estimator_\n",
    "best_lr_pred = best_lr_model.predict(X_test)\n",
    "best_acc_lr = accuracy_score(y_test, best_lr_pred)\n",
    "best_acc_lr\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳随机森林\n",
      "RandomForestClassifier(max_depth=60, max_samples=0.9, n_estimators=250)\n",
      "最佳参数\n",
      "{'max_depth': 60, 'max_samples': 0.9, 'n_estimators': 250}\n",
      "权重系数\n",
      "                   features      权重系数\n",
      "0  Daily Time Spent on Site  0.099492\n",
      "1                       Age  0.254051\n",
      "2               Area Income  0.107891\n",
      "3      Daily Internet Usage  0.120173\n",
      "4             Ad Topic Line  0.128372\n",
      "5                      City  0.149204\n",
      "6                    Gender  0.016954\n",
      "7                   Country  0.123862\n"
     ]
    },
    {
     "data": {
      "text/plain": "['best_rf_model.pkl']"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('最佳随机森林')\n",
    "print(best_rf_model)\n",
    "\n",
    "print('最佳参数')\n",
    "print(rf_gridCV.best_params_)\n",
    "\n",
    "print('权重系数')\n",
    "a = pd.DataFrame({'features': X.columns, '权重系数': best_rf_model.feature_importances_})\n",
    "\n",
    "print(a)\n",
    "\n",
    "#保存最佳模型\n",
    "import joblib\n",
    "\n",
    "joblib.dump(best_rf_model, 'best_rf_model.pkl')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}